﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using intelliSys.NankaiUtilities.EventSynchronizer;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;

namespace intelliSys.NankaiUtilities.EventSynchronizer
{
    public static class Helpers
    {
      
        public static Regex regTime = new Regex("((\\d+)[年\\.\\-]){0,1}(\\d+)[月\\.\\-](\\d{1,2})[日号]*[：: ]*((\\d+)([:：\\.点](\\d+)){0,1})*");
        public static string SynchronizedEventFilePath = "synchronized.xml";
        public static List<string> SynchronizedEventNames;
        public const string ApiUri = @"https://apis.live.net/v5.0/";
        public static string AccessToken;
        public const string ClientID = "00000000440ED616";
        public const string Scope = "wl.calendars_update,wl.events_create,wl.signin";
        public const string RedirectUri = "http://www.liangluo.net/2013/04/11/Chem-Lecture-Event-Notifier-For-Live-Calendar-NKU/";
        public const string ClientSecret = "OXqIscR3MW5CWRQyeChZlkB3afip2yoF";
        public static string AuthCode;
        public static string GetSignInUri()
        {
            return "https://login.live.com/oauth20_authorize.srf?client_id=" + ClientID + "&scope=" + Scope + "&response_type=code&redirect_uri=" + RedirectUri;
        }

        public static string GetAccessTokenRequestURI()
        {
            return string.Format(String.Format("https://login.live.com/oauth20_token.srf?client_id={0}&client_secret={1}&redirect_uri={2}&grant_type=authorization_code&code={3}", ClientID, ClientSecret, RedirectUri, AuthCode));
        }

        public static string CreateJSONReadyDateTimeString(DateTime dt)
        {
            dt = dt.AddHours(-8);
            return string.Format("{0}-{1}-{2}T{3}:{4}:00-0000", dt.Year.ToString().PadLeft(4, '0'), dt.Month.ToString().PadLeft(2, '0'), dt.Day.ToString().PadLeft(2, '0'), dt.Hour.ToString().PadLeft(2, '0'), dt.Minute.ToString().PadLeft(2, '0'));
        }

        public static string LiveEventToJSON(LiveEvent liveEvent, SyncOptions syncOptions)
        {
            Dictionary<string, string> jsonContentMaker = new Dictionary<string, string>();
            jsonContentMaker.Add("name", liveEvent.Title);
            jsonContentMaker.Add("description", liveEvent.Description);
            jsonContentMaker.Add("start_time", Helpers.CreateJSONReadyDateTimeString(liveEvent.StartTime));//");//Constants.CreateJSONReadyDateTimeString(liveEvent.StartTime));
            //jsonContentMaker.Add("end_time", Constants.CreateJSONReadyDateTimeString(liveEvent.EndTime));
            jsonContentMaker.Add("location", liveEvent.Location);
            //jsonContentMaker.Add("is_all_day_event", "false");
            //jsonContentMaker.Add("availability", syncOptions.Availability);
            //jsonContentMaker.Add("visibility", syncOptions.Visibility);
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(jsonContentMaker);
        }
    }
}
